test_description="quarantine-helper filter"

. ./lib/sharness.sh

Quaranine="*incoming*"

test_expect_success "setup" '
	git init --bare remote.git &&
	(
		cd remote.git &&
		git config transfer.unpacklimit 10
	) &&
	cp "$HOME/../resources/pre-receive" remote.git/hooks/
'

test_expect_success "scan quarantine(one commit)" '
    cp -r remote.git tmp-remote.git &&
    git clone --no-local remote.git local &&
	(
		cd local &&
		generate_commits 0 0 &&
		git push
	) &&
	cat >expect<<-EOF &&
	6201bff8206a22207277d0e64bac92ec01f5cb10
	EOF
	GIT_DIR=local/.git quarantine-helper scan -q $Quaranine | \
	quarantine-helper filter -q $Quaranine -r tmp-remote.git -b 6201bff8206a22207277d0e64bac92ec01f5cb10>actual &&
	test_cmp expect actual
'

test_expect_success "scan quarantine(5 commits)" '
	rm -rf $Quaranine &&
    cp -r remote.git tmp-remote.git &&
	(
		cd local &&
		generate_commits 1 5 &&
		git push
	)
	cat >expect<<-EOF &&
	4272e5f09914017057bc328da23de529a23dc946
	1b92441c9aeca6a9b4b1df3f24e2ecbea7ec4096
	c70a396ef27abad5199144f6287a3b891e097c11
	042f968d17d76f896591fe05454208ecd64291a5
	54a2d8b0c097f71e3e43c046ad2beb51bbbf2955
	EOF
	GIT_DIR=local/.git quarantine-helper scan -q $Quaranine | \
	quarantine-helper filter -q $Quaranine -r tmp-remote.git -b 4272e5f09914017057bc328da23de529a23dc946 >actual &&
	test_cmp expect actual
'


test_expect_success "new branch with no new commits" '
    rm -rf $Quaranine &&
	rm -rf tmp-remote.git &&
    cp -r remote.git tmp-remote.git &&
	(
		cd local &&
		git checkout -b topic1&&
		git push -u origin topic1
	) &&
	GIT_DIR=local/.git quarantine-helper scan -q $Quaranine | \
	quarantine-helper filter -q $Quaranine -r tmp-remote.git -b 4272e5f09914017057bc328da23de529a23dc946 >actual &&
	test -f actual && test ! -s actual
'


test_expect_success "push multiple branch at the same time" '
    rm -rf $Quaranine &&
	rm -rf tmp-remote.git &&
    cp -r remote.git tmp-remote.git &&
	(
		cd local &&
		git checkout -b fix0  master &&
		git checkout -b fix1  master &&
		generate_commits 30 32 &&
		git checkout -b fix2  master &&
		generate_commits 33 34 &&
		git checkout -b fix3  master &&
		generate_commits 35 35 &&
		git push --all
	) &&

    #fix0
	GIT_DIR=local/.git quarantine-helper scan -q $Quaranine | \
	quarantine-helper filter -q $Quaranine -r tmp-remote.git -b 4272e5f09914017057bc328da23de529a23dc946 >actual &&
	test -f actual && test ! -s actual
'

test_expect_success "push multiple branch at the same time" '
    #fix1
    cat >expect<<-EOF &&
	e4d1b749701d7e5f47df479ebdd752ae11310044
	c86119e692ca7db5d63747ce2042730df1cd6bab
	d149052326d311e6dd2c449f536f30ab13b2109c
	EOF
    GIT_DIR=local/.git quarantine-helper scan -q $Quaranine | \
	quarantine-helper filter -q $Quaranine -r tmp-remote.git -b e4d1b749701d7e5f47df479ebdd752ae11310044 >actual &&
    test_cmp expect actual
'

test_expect_success "push multiple branch at the same time" '
    #fix2
    cat >expect<<-EOF &&
	3998b99b8b99fd5936f9c57c9d7d2b51ef880537
	48c16d6042cd71984f0e33d1b40b6bc39c87398f
	EOF
    GIT_DIR=local/.git quarantine-helper scan -q $Quaranine | \
	quarantine-helper filter -q $Quaranine -r tmp-remote.git -b 3998b99b8b99fd5936f9c57c9d7d2b51ef880537 >actual &&
    test_cmp expect actual
'

test_expect_success "push multiple branch at the same time" '
    #fix3
    cat >expect<<-EOF &&
	19ee49f0b2d4f0dd13d9fdf7257f022af8a9687b
	EOF
    GIT_DIR=local/.git quarantine-helper scan -q $Quaranine | \
	quarantine-helper filter -q $Quaranine -r tmp-remote.git -b 19ee49f0b2d4f0dd13d9fdf7257f022af8a9687b >actual &&
	test_cmp expect actual
'

test_expect_success "new branch with new commits" '
    rm -rf $Quaranine &&
	rm -rf tmp-remote.git &&
    cp -r remote.git tmp-remote.git &&
	(
		cd local &&
		git checkout -b topic2 master&&
		generate_commits 26 28 &&
		git push -u origin topic2
	) &&
	cat >expect<<-EOF &&
	21f2933c8f214c99eae6f38b8c8bb9c3fe266dff
	10ae3395d7419f9ffea89f165f49ed1e4fdb9da2
	cfb0f5202fe5dcebf53322709266c54d4bb4abca
	EOF
    GIT_DIR=local/.git quarantine-helper scan -q $Quaranine | \
	quarantine-helper filter -q $Quaranine -r tmp-remote.git -b 21f2933c8f214c99eae6f38b8c8bb9c3fe266dff >actual &&
	test_cmp expect actual
'

test_expect_success "merge" '
    rm -rf $Quaranine &&
	rm -rf tmp-remote.git &&
    cp -r remote.git tmp-remote.git &&
	(
		cd local &&
		git checkout -b topic3  master &&
		generate_commits 29 29 &&
		git merge topic2 &&
		git push -u origin topic3
	) &&
	cat >expect<<-EOF &&
	c11a56907863f54a639abdf269902e01c21e76e3
	0fa6d1a6ff6794110845d28b1990afe27d11d703
	EOF
    GIT_DIR=local/.git quarantine-helper scan -q $Quaranine | \
	quarantine-helper filter -q $Quaranine -r tmp-remote.git -b c11a56907863f54a639abdf269902e01c21e76e3 >actual &&
	test_cmp expect actual
'

test_expect_success "existing detached commits in remote repository" '
    rm -rf $Quaranine &&
	rm -rf tmp-remote.git &&
    cp -r remote.git tmp-remote.git &&
	(
		cd local &&
		git checkout -b rollback master &&
		generate_commits 150 151 &&
        git push -u origin rollback &&
        git checkout -b normal &&
        generate_commits 152 152 &&
        git checkout rollback &&
        git reset --hard master &&
        git push -f --all

	) &&
    GIT_DIR=local/.git quarantine-helper scan -q $Quaranine | \
	quarantine-helper filter -q $Quaranine -r tmp-remote.git -b b735449c594ce6d148f4640792d5bbae2cddb1d5 >actual &&
	test -f actual && test ! -s actual
'

test_expect_success "two parents and amount of commits is more than limit, check if order is right" '
    rm -rf $Quaranine &&
	rm -rf tmp-remote.git &&
    cp -r remote.git tmp-remote.git &&
	(
		cd local &&
		git checkout -b parent0 master &&
        generate_commits 30 36 &&
        git checkout -b parent1 master &&
        generate_commits 37 43 &&
        git checkout -b child&&
		GIT_MERGE_AUTOEDIT=no git merge parent0 &&
		git push -u origin child
	) &&
	cat >expect<<-EOF &&
	08339df6d09fd768cb88acc13d2692255096882b
	1a66924b1e2c86f00bfe10e5ae76559664edd18c
	671cd540b5266181dc68532ace0d0ac587b66fe9
	6bb73e1146e8dd98834b2cd8f4079ca4cc3cd300
	aee6c55fea730fa57575d8950393281ed0ba738b
	9ba3d9df867519198c9f1433e276eb3efefc01bc
	aed59813b1682fe28d9d0dfd5ed063380fff0709
	2fef73308f46d031c1ab57ab4b083c9dd09745cf
	5fe812f6690b56a0e0ccec2cfb568615f83e22df
	2590b7e51f9b571a15a16959fd75dffd156369df
	EOF
    GIT_DIR=local/.git quarantine-helper scan -q $Quaranine | \
	quarantine-helper filter -q $Quaranine -r tmp-remote.git -b 08339df6d09fd768cb88acc13d2692255096882b -l 10 >actual &&
	test_cmp expect actual
'
test_done